/*!
 * Distpicker v1.0.4
 */

/**********************省市三级联动
(function (factory) {

    if (typeof define === 'function' && define.amd) {
        // AMD. Register as anonymous module.
        define('ChineseDistricts', [], factory);
    } else {
        // Browser globals.
        factory();
    }

})

(function () {

    var ChineseDistricts = "";
    $.ajax({
        type: "GET",
        contentType: 'application/json',
        dataType: "json",
        async: false,
        url: "commonapi/region",
        success: function (jsonData) {
            if (jsonData.code == 200) {
                ChineseDistricts = jsonData.map;
            }
        }
    });


    if (typeof window !== 'undefined') {
        window.ChineseDistricts = ChineseDistricts;
    }

    return ChineseDistricts;

});
 **********************/


/******************************三级联动end***********************/


/******************************地区四级联动srart***********************/

$(function () {
    openProvinceList();
    queryaddress_sslx();
});




//查询省份信息---支持到街道
function openProvinceList() {
    $.ajax({
        type: "GET",
        async: false,
        url: "commonapi/province", //数据库查询数据
        dataType: 'json',
        success: function (jsonData) {
            if (jsonData.code == 200) {
                $(".province option[index != '0']").remove();
                if (jsonData.list.length > 0) {
                    $(".province").append("<option value=\"\" >--请选择--</option>");
                    $.each(jsonData.list, function (index, provinceList) {
                        $(".province").append("<option value=" + provinceList.dzbm + ">" + provinceList.dzmc + "</option>");
                    });
                }
                //openCityList();
                // openCountyList();
                // openTownList();
            }
        }
    })
}

//1 直辖市   0  非直辖市
var sslx="";
function queryaddress_sslx() {
    $.ajax({
        type:"GET",
        dataType:"json",
        url:"commonapi/address_sslx",
        success:function (jsonData) {
            //1 直辖市   0  非直辖市
            sslx=jsonData.sysDzxxEntity.sslx;
        }
    });
}

//查询对应的城市信息
function openCityList() {
    var provinceCode = $(".province option:selected").val();
    var queryParams={provinceCode:provinceCode,sslx:sslx};
    if (provinceCode) {
        $.ajax({
            type: "GET",
            async: false,
            url: "commonapi/city", //数据库查询数据
            dataType: 'json',
            data: queryParams,
            success: function (jsonData) {
                if (jsonData.code == 200) {
                    $(".city option[index != '0']").remove();
                    if (jsonData.citylist.length > 0) {
                        $(".city").append("<option value=\"\" >--请选择--</option>");
                        $.each(jsonData.citylist, function (index, list) {
                            $(".city").append("<option value=" + list.dzbm + ">" + list.dzmc + "</option>");
                        });
                    }
                    openCountyList();
                    openTownList();
                }
            }
        });
    } else {
        $(".county option[index !=0]").remove();
        $(".street option[index !=0]").remove();
    }
}

//查询对应的县区信息
function openCountyList() {
    var provinceCode = $(".province option:selected").val();
    var cityCode = $(".city option:selected").val();
    if (provinceCode) {
        if (cityCode) {
            $.ajax({
                type: "GET",
                async: false,
                url: "commonapi/county/" + cityCode, //数据库查询数据
                dataType: 'json',
                success: function (jsonData) {
                    if (jsonData.code == 200) {
                        $(".county option[index != '0']").remove();
                        if (jsonData.countylist.length > 0) {
                            $(".county").append("<option value=\"\" >--请选择--</option>");
                            $.each(jsonData.countylist, function (index, list) {
                                $(".county").append("<option value=" + list.dzbm + ">" + list.dzmc + "</option>");
                            });
                        }
                        openTownList();
                    }
                }
            });
        } else {
            $(".county option[index !=0]").remove();
            $(".street option[index !=0]").remove();
        }
    } else {
        $(".city option[index != 0]").remove();
    }
}

//查询对应的街道，镇信息
function openTownList() {
    var provinceCode = $(".province option:selected").val();
    var cityCode = $(".city option:selected").val();
    var countyCode = $(".county option:selected").val();
    if (provinceCode) {
        if (cityCode) {
            if (countyCode) {
                $.ajax({
                    type: "GET",
                    url: "commonapi/street/" + countyCode, //数据库查询数据
                    dataType: 'json',
                    async: false,
                    success: function (jsonData) {
                        if (jsonData.code == 200) {
                            $(".street option[index != '0']").remove();
                            if (jsonData.streetlist.length > 0) {
                                $(".street").append("<option value=\"\" >--请选择--</option>");
                                $.each(jsonData.streetlist, function (index, list) {
                                    $(".street").append("<option value=" + list.dzbm + ">" + list.dzmc + "</option>");
                                });
                            }
                        }
                    }
                });
            } else {
                $(".street option[index !=0]").remove();
            }
        } else {
            $(".county option[index !=0]").remove();
            $(".street option[index !=0]").remove();

        }
    } else {
        $(".city option[index !=0]").remove();
    }
}
/******************************地区四级联动srart***********************/

